package com.jiang.daily.Q498;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/08/25 10:47
 */
class Solution {
    public int[] findDiagonalOrder(int[][] mat) {
        int n = mat.length;
        int m = mat[0].length;
        int[] res = new int[m * n];
        int index = 0;
        int dir = -1;
        for (int i = 0, j = 0; i < n && j < m;) {
            res[index++] = mat[i][j];
            int r = i + dir;
            int c = j - dir;
            if (r >= 0 && r < n && c >= 0 && c < m) {
                i += dir;
                j -= dir;
                continue;
            } else if (r < 0 && c == m) {
                i++;
            } else if (r == n && c < 0){
                j++;
            } else if (r < 0 || r == n) {
                j++;
            } else if (c < 0 || c == m) {
                i++;
            }
            dir *= -1;
        }
        return res;
    }
}
